package com.hbsd.paopaobackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbsd.paopaobackend.common.BaseResponse;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.common.ResultUtils;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.model.domain.Message;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.Message.MessageListRequest;
import com.hbsd.paopaobackend.model.vo.Message.MessageListResponse;
import com.hbsd.paopaobackend.service.MessageService;
import com.hbsd.paopaobackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "聊天模块")
@RestController
@RequestMapping("/message")
// 配置跨域 allowCredentials: 允许携带cookie
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")

public class MessageController {
    
    @Resource
    private UserService userService;

    @Resource
    private MessageService messageService;


    /**
     * 发送消息
     * @param message
     * @param request
     * @return
     */
    @ApiOperation(value = "发送消息", notes = "发送消息 send")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Cookie", value = "Cookie", dataType = "string", paramType = "header")}
    )
    @PostMapping("/send")
    public BaseResponse<Boolean> send(@RequestBody Message message , HttpServletRequest request) {
        if(message == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "消息不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Boolean result = messageService.send(message, loginUser);

        return ResultUtils.success(result);
    }

    /**
     * 获取消息列表
     * @param messageListRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "获取消息列表", notes = "获取消息列表 listMessagePage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Cookie", value = "Cookie", dataType = "string", paramType = "header")}
    )
    @PostMapping("/list/page")
    public BaseResponse<Page<MessageListResponse>> listMessagePage(@RequestBody MessageListRequest messageListRequest, HttpServletRequest request) {
        if(messageListRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Page<MessageListResponse> page = messageService.listMessagePage(messageListRequest, loginUser);

        return ResultUtils.success(page);

    }
}
